package com.itheima.bos.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.dao.system.MenuRepository;
import com.itheima.bos.domain.system.Menu;
import com.itheima.bos.domain.system.User;
import com.itheima.bos.service.system.IMenuService;

@Service
@Transactional
public class MenuServiceImpl implements IMenuService {
	
	@Autowired
	private MenuRepository  menuRepository;

	@Override
	public List<Menu> findAll() {
		
		return menuRepository.findAll();
	}

	@Override
	public void save(Menu model) {
		//防止用户没有选中父菜单11111111111111111111111111111
		if (model.getParentMenu()!=null&&model.getParentMenu().getId()==0) {
			model.setParentMenu(null);
		}
		menuRepository.save(model);
	}

	@Override
	public List<Menu> findByUser(User user) {
		
		if (user.getUsername().equals("admin")) {
			//所有的菜单页面都会显示
			return menuRepository.findAll();
		}else {
			//否则根据登录有户的id查询对应的菜单页面
			return menuRepository.findByUser(user.getId());
			
		}
		
		
		
	}
	
	
	
}
